// pages/module/addressBook/addressBook.js
import regeneratorRuntime from '../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  checkSelfInfo,
  getGroupList,
  refreshInfo,
  update_memberlevel_manual,
  cherckMineInfo,
} from '../../../utils/imapi.js';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',
    show_part:false,
    wx_no:'',
	  index:1,
    list:[],
	  module:false,
	  hex_code:false, //是否显示提示
	  time_order_desc:'',  //注册时间升降序
	  team_cnt_order_desc:'', //成员数量升降序
	  hex_time:0, //选中的条件
	  num_theuser:'',
	  num_twitter:'',
	  num_mentor:'',
	  search_name:'',  //搜索填入的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {

    const selfInfo = await checkSelfInfo();
    const {data} = await getGroupList({page_index:1,level_code:this.data.hex_time});
	console.log(data.Members);
    try {
      var wx_no = await getData(dataKey.wx_no);
    } catch (error) {
      
    }
	var date=await cherckMineInfo();
	
    this.setData({
      wx_no,
      user_list:selfInfo.data,
      userInfo:date.data,
      newAdd:data.new_num,
      allFans:data.total_num,
      direct_num:data.direct_num,
      list:data.Members,
	  num_theuser:data.level_num[0],
	  num_twitter:data.level_num[1],
	  num_mentor:data.level_num[2]
    })
	
	if(!this.data.userInfo.wechat_alian_no){
		this.setData({
			hex_code:true
		})
	}

  },
  async hex_time(e){
	  console.log(e);
	  this.setData({
	  	   hex_time:e.currentTarget.dataset.index
	  })
	  const {data} = await getGroupList({page_index:1,time_order_by:this.data.time_order_desc,team_cnt_order_by:this.data.team_cnt_order_desc,level_code:this.data.hex_time});
	  this.setData({
		   list:data.Members
	  })
	  
  },
    hex_search(e){
	   this.setData({
		   search_name : e.detail.value
	   })
  },
  async hex_search_ss(){
	  const {data} = await getGroupList({page_index:1,time_order_by:this.data.time_order_desc,team_cnt_order_by:this.data.team_cnt_order_desc,level_code:this.data.hex_time,member_name:this.data.search_name});
	  this.setData({
	  		   list:data.Members
	  })
  },
  async time_order_time(){
    this.setData({
      team_cnt_order_desc:''
    })
	  if(this.data.time_order_desc=='desc'){
		  const {data} = await getGroupList({page_index:1,time_order_by:'asc',team_cnt_order_by:this.data.team_cnt_order_desc,level_code:this.data.hex_time});
		  this.setData({
			  time_order_desc:'asc', 
			  list:data.Members,
		  })
	  }else{
		  const {data} = await getGroupList({page_index:1,time_order_by:'desc',team_cnt_order_by:this.data.team_cnt_order_desc,level_code:this.data.hex_time});
		  this.setData({
		  			  time_order_desc:'desc', 
		  			  list:data.Members
		  })
	  }
	  
  },
  async time_order_count(){
    this.setData({
      time_order_desc:''
    })
	  if(this.data.team_cnt_order_desc=='desc'){
	  		  const {data} = await getGroupList({page_index:1,time_order_by:this.data.time_order_desc,team_cnt_order_by:'asc',level_code:this.data.hex_time});
	  		  this.setData({
	  			  team_cnt_order_desc:'asc', 
	  			  list:data.Members
	  		  })
	  }else{
	  		  const {data} = await getGroupList({page_index:1,time_order_by:this.data.time_order_desc,team_cnt_order_by:'desc',level_code:this.data.hex_time});
	  		  this.setData({
	  		  			  team_cnt_order_desc:'desc', 
	  		  			  list:data.Members
	  		  })
	  }
  },
  jump_orders_rpt(e){
    let item = e.currentTarget.dataset.item;
	  wx.navigateTo({
	    url: '/packageTab/pages/Fansoftheorder/Fansoftheorder?member_id='+item.id
	  })
  },
  showMobile(){
    this.setData({
      show_part:true
    })
  },
  async more(){
	  this.setData({
		  module:true
	  });
	  var index=this.data.index+1;
	 const {data} = await getGroupList({page_index:index,level_code:this.data.hex_time});
	 console.log(data);
   
   if(data.Members==null){
	   this.setData({
	    	module:false
	   })
     return 
   }
   
	 data.Members.forEach(res=>{
		this.data.list.push(res)
	 })
   console.log(this.data.list);
   
   setTimeout(()=>{
	this.setData({
		list:this.data.list,
		index:index,
		module:false
	})
   },1000);	
	
  },
  hideShare(){
    this.setData({
      show_part:false
    })
  },
  changeInput(e){
    this.data.wx_no = e.detail.value
  },
  async confirm(){
    if(!this.data.wx_no){
      wx.showToast({
        icon:'none',
        title: '请输入微信号',
      })
      return;
    }
    await setData(dataKey.wx_no,this.data.wx_no);
    refreshInfo({wechat_alian_no:this.data.wx_no})
    app.globalData.userInfo.wechat_alian_no = this.data.wx_no //update by finder
	var date=await cherckMineInfo();
	
    this.setData({
      wx_no:this.data.wx_no,
      show_part:false,
	  userInfo:date.data
    })
	
  },
  bindDownLoad:function(){
     console.log('滚到底部');
	 if(!this.data.module){
		 this.more();
	 }
    },
    scroll:function(event){
       //console.log('滚到底部的图')
	   //this.more();
    },
	livecop(e){
		  var text = e.currentTarget.dataset.text;
		  wx.setClipboardData({
		        data: text,
		        success: function (res) {
		         wx.showToast({
		          title: '复制成功',
		          icon: 'success',
		          duration: 2000
		         })
		   
		        }
		      })
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
	  console.log(app.globalData.userInfo.wechat_alian_no);
	  var date=await cherckMineInfo();
     if(!date.data.wechat_alian_no){
        wx.showToast({
          title: '请填写微信哦',
          duration:3000
        })
     }
  },



  copy(e){
    var text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success() {
        wx.showToast({
          title: '复制成功'
        })
      },
    })
  },
 

  
  

  fanUpgrade(e){
      console.log(e);
  
      let selected_level_code=e.currentTarget.dataset.item.level_code
  
      if(selected_level_code==3){
        wx.showModal({
            title: '提示',
            content: '会员等级已为最高',
          })
        return;
      }else if(selected_level_code==2){
        wx.showModal({
          title: '提示',
          content: '不可以手动升级了',
        })
        return;
      }else{
  
      // 自己的等级
      if (app.globalData.userInfo.level_code<=selected_level_code){
  
        wx.showModal({
          title: '提示',
          content: '不可以提升超过自己的级别',
        })
        return;
      }
  
        var level_code=selected_level_code;
        var id=e.currentTarget.dataset.item.id;
        var code_id=String(Number(level_code)+1);
        var code='';
        if(level_code=='0'){
          code='推客'
        }else if(level_code=='1'){
          code='导师'
        }
        wx.showModal({
            title: '提示',
            content: '把 "'+e.currentTarget.dataset.item.name+'" 升级为 "'+code+'" ？',
            success:(res)=>{
            if(res.cancel){
              
            }else{
              this.upda(level_code,id,code_id,e.currentTarget.dataset.index);
            }
          }
        
      })
      
      }
    },
    async upda(level_code,id,code_id,item_index){
      const data = await update_memberlevel_manual({from_level_code:level_code,member_id:id,level_code:code_id});
      console.log(data);
      if(data.code==0){
            if(data.data==-1){
          wx.showModal({
              title: '提示',
              content:  data.msg
          })
          // console.log('测试的');
          // const date=await top_submembers({page_index:1,page_size:this.data.page_size});
          // console.log(date);
          // this.setData({
          // 	list:date.data
          // });
        }else{
          wx.showToast({
              title: '操作成功！',  // 标题
              icon: 'success',   // 图标类型，默认success
              duration: 1500   // 提示窗停留时间，默认1500ms
          })
          let list=this.data.list
          // let item = list[item_index]
           list[item_index].level_code = code_id
           this.setData({
               list:list
           })
        }
      }else{
        wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 1500
        })
      }
      
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(!this.data.module){
      		 this.more();
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	if(!app.is_auth()){
		  wx.showToast({
			title: '您还未登陆，请登陆后再分享可以赚佣金哦',
		  })
		}
    return shareFun.shareIndex(app.globalData.userInfo.uid,app.globalData.userInfo.wx_nickname);
  }
})